package com.qinggeng.project.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.qinggeng.common.constant.Constants;
import com.qinggeng.common.enums.*;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.PositionDto;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.Company;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.domain.Position;
import com.qinggeng.project.system.service.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.testng.collections.Maps;

import java.util.Map;

/**
 * @author smallfish
 *
 */
@Api(tags = "职位模块")
@RestController
@CrossOrigin
@RequestMapping("${apiPath}/position")
public class ApiPositionController extends BaseController {

    private static final String POSITION_TYPE="[{'value':'10','name':'全职'},{'value':'20','name':'兼职'}]";

    @Autowired
    private IPositionService positionService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IJobUserService jobUserService;

    @Autowired
    private IJobInfoCollectService jobInfoCollectService;

    @Autowired
    private IJobInfoBrowseService jobInfoBrowseService;
    @Autowired
    private JobDliveryService jobDliveryService;
    @Autowired
    private JobMessageService jobMessageService;
    @Autowired
    private ICompanyService companyService;

    @ApiOperation("岗位列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "keyword" ,value = "职位名称关键词"),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @LoginRequired(must = false)
    public AjaxResult positionList(Position position,String keyword){
        startPage();
        position.setName(keyword);
        return AjaxResult.success(positionService.listPositionDto(position));
    }

    @ApiOperation("我的职位管理")
    @GetMapping("/myList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = true)
    })
    @LoginRequired()
    public AjaxResult myPositionList(Position where){
        startPage();
        where.setJobUserId(jobUserService.getJobUserId());
        return AjaxResult.success(positionService.listPosition(where));
    }

    @ApiOperation("编辑职位")
    @PostMapping("/edit")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = true)
    })
    @LoginRequired()
    public AjaxResult addPosition(Position position){
        JobUser jobUser=jobUserService.getJobUser();
        //查询公司
        Company company=companyService.getCompanyByIdAndJobUserId(jobUser,position.getCompanyId());
        if(null==company.getStatus()||!company.getStatus().equals(CheckStatusEnum.已通过.getValue())){
            return AjaxResult.error("公司等待审核中，暂不能编辑职位！",ShowTypeEnum.用户.getValue());
        }
        //设置职位分类
        position.setClassifyCode(company.getClassifyCode());
        position.setPlace(company.getAddress()+company.getAddressInfo());
        //新增职位
        if(null==position.getId()){
            position.setJobUserId(jobUser.getJobUserId());
            return toAjax(positionService.insertPosition(position));
        }
        //更新职位
        if(null==position.getJobUserId()||position.getJobUserId().compareTo(jobUser.getJobUserId())!=0){
            return AjaxResult.error("没有权限修改！",ShowTypeEnum.用户.getValue());
        }
        return toAjax(positionService.updatePosition(position));
    }

    @ApiOperation("删除职位")
    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "职位id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = true)
    })
    @LoginRequired()
    public AjaxResult deletePosition(Long id){
        positionService.getPositionByIdAndJobUserId(id);
        return toAjax(positionService.deletePositionById(id));
    }

    @ApiOperation("岗位详情")
    @GetMapping("/info")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "职位id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @LoginRequired(must = false)
    public AjaxResult positionInfo(Long id){
        PositionDto positionDto=positionService.getPositionDtoById(id);
        if(null==positionDto.getStatus()||!positionDto.getStatus().equals(CheckStatusEnum.已通过.getValue())){
            return AjaxResult.error("等待审核通过",ShowTypeEnum.用户.getValue());
        }

        if(ApiUtils.getIsLogin()){
            Long jobUserId=jobUserService.getJobUserId();
            //添加浏览记录
            jobInfoBrowseService.updateJobInfoBrowse(jobUserId,id,JobInfoTypeEnum.POSITION_INFO.getType());
            positionDto.setIsCollect(jobInfoCollectService.checkJobInfoIsCollect(id,JobInfoTypeEnum.POSITION_INFO.getType()));
            positionDto.setIsDelivery(jobDliveryService.checkPositionIsDelivery(jobUserId,id));
        }
        return AjaxResult.success(positionDto);
    }

    @ApiOperation("职位收藏")
    @GetMapping("/collect")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "职位id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult positionCollect(Long id){
        positionService.getPositionDtoById(id);
        Long jobUserId=jobUserService.getJobUserId();
        return toAjax(jobInfoCollectService.updateJobInfoCollect(jobUserId,id, JobInfoTypeEnum.POSITION_INFO.getType()));
    }


    @ApiOperation("收藏列表")
    @GetMapping("/collectList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult collectList(){       ;
        return AjaxResult.success( positionService.listCollectPositionDto());
    }


    @ApiOperation("岗位浏览记录")
    @GetMapping("/browse")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "职位id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @LoginRequired(must = false)
    public AjaxResult browseRecord(Long id){
        Map<String,Object> data=Maps.newHashMap();
        data.put("count",positionService.getPositionBrowseCountById(id));
        data.put("list",jobInfoBrowseService.listJobInfoBrowseUserByJobInfoIdAndBrowseType(id,JobInfoTypeEnum.POSITION_INFO.getType()));
        return AjaxResult.success(data);
    }


    @ApiOperation("我的浏览记录")
    @GetMapping("/myBrowse")
    @ApiImplicitParams({
         @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult myBrowseRecord(){
        return AjaxResult.success(positionService.listBrowsePositionDto());
    }


    @ApiOperation("统计数量")
    @GetMapping("/count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult count(){
        Map<String,Object> data=Maps.newHashMap();
        data.put("collectCount",positionService.getPositionCollectCountByUserId());
        data.put("deliveryCount",jobDliveryService.getJobDeliveryCountByPositionUserId());
        data.put("messageCount",jobMessageService.getJobMessageCountByPositionUserId());
        return AjaxResult.success(data);
    }


    @ApiOperation("配置文件")
    @GetMapping("/config")
    public AjaxResult config(){
        Map<String,Object> data=Maps.newHashMap();
        data.put("salary",configService.getConfigValue("sys.job.salary").split(","));
        data.put("education",configService.getConfigValue("sys.job.education").split(","));
        data.put("experience",configService.getConfigValue("sys.job.experience").split(","));
        data.put("scale", configService.getConfigValue("sys.company.scale").split(","));
        data.put("benefits", configService.getConfigValue("sys.company.benefits").split(","));
        data.put("type", JSONObject.parseArray(POSITION_TYPE));
        return AjaxResult.success(data);
    }


}
